/**
 * 身份认证API服务
 * 处理登录、注册、登出等认证相关请求
 * @author LYC
 * @date 2025-09-21
 */

export interface LoginCredentials {
  username: string
  password: string
}

export interface LoginResponse {
  id: string
  username: string
  nickname: string
  avatar: string
  status: 'online' | 'offline' | 'busy' | 'away'
  email?: string
  token: string
}

export interface ApiError {
  code: string
  message: string
}

/**
 * 登录API
 * @param credentials 登录凭证
 */
export async function login(credentials: LoginCredentials): Promise<LoginResponse> {
  try {
    // TODO: 替换为实际的API调用
    console.log('调用登录API:', credentials)

    // 模拟API调用延迟
    await new Promise((resolve) => setTimeout(resolve, 1000))

    // 模拟API响应
    // 当实际API准备好后，替换此处代码
    if (credentials.username === 'admin' && credentials.password === 'admin123') {
      return {
        id: '1001',
        username: credentials.username,
        nickname: '管理员',
        avatar:
          'https://images.unsplash.com/photo-1535713875002-d1d0cf377fde?w=150&h=150&fit=crop&crop=center',
        status: 'online',
        email: 'admin@example.com',
        token: 'mock-jwt-token-' + Date.now()
      }
    } else {
      throw {
        code: 'AUTH_FAILED',
        message: '用户名或密码错误'
      }
    }
  } catch (error) {
    console.error('登录请求失败:', error)
    throw error
  }
}

/**
 * 登出API
 */
export async function logout(): Promise<void> {
  try {
    // TODO: 替换为实际的API调用
    console.log('调用登出API')

    // 模拟API调用延迟
    await new Promise((resolve) => setTimeout(resolve, 500))

    // 清理本地存储的登录态
    localStorage.removeItem('auth-token')

    return
  } catch (error) {
    console.error('登出请求失败:', error)
    throw error
  }
}

/**
 * 验证令牌有效性API
 * @param token JWT令牌
 */
export async function verifyToken(token: string): Promise<boolean> {
  try {
    // TODO: 替换为实际的API调用
    console.log('验证令牌:', token)

    // 模拟API调用延迟
    await new Promise((resolve) => setTimeout(resolve, 300))

    // 模拟验证逻辑
    return token.startsWith('mock-jwt-token-')
  } catch (error) {
    console.error('令牌验证失败:', error)
    return false
  }
}
